package com.miao.manage.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.miao.entity.PageVo;
import com.miao.entity.TbContent;
import com.miao.service.TbContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shuangshuang
 * @since 2018-09-07
 */
@Controller
@RequestMapping("/tbContent")
public class TbContentController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TbItemCatController.class);
    @Autowired
    private TbContentService tbContentService;

    /**
     * 分页查询内容管理
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<PageVo> querycontent(@RequestParam(defaultValue = "1",name = "page") Integer pageNum,Integer rows, Integer categoryId){

        Page<TbContent> tbcon = new Page<>(pageNum, rows);
        EntityWrapper<TbContent> wrapper = new EntityWrapper<>();
        wrapper.orderBy("updated DESC");
        wrapper.where("category_id={0}",categoryId);
        Page<TbContent> tbContentPage = tbContentService.selectPage(tbcon, wrapper);
        //EasyUI 分页插件
        PageVo<TbContent> pageVo = new PageVo<>();
        pageVo.setTotal((int)tbcon.getTotal());
        pageVo.setRows(tbcon.getRecords());
        return ResponseEntity.ok(pageVo);
    }

    /**
     * 添加内容管理
     * @param tbContent
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<TbContent> addcontent(TbContent tbContent){
        try{
            if(StringUtils.isEmpty(tbContent)){
                //为空
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            tbContent.setCreated(new Date());
            tbContent.setUpdated(new Date());
            tbContentService.insert(tbContent);
            //添加成功
            return ResponseEntity.status(HttpStatus.CREATED).build();
        }catch (Exception e){
            LOGGER.error("新增商品失败！tbItem="+tbContent,e);
        }
        //添加失败！
        return ResponseEntity.status(HttpStatus.INSUFFICIENT_STORAGE).build();
    }

    /**
     * 修改
     * @param tbContent
     * @return
     */
    @RequestMapping("/UpdateContent")
    public ResponseEntity<TbContent> upcontent(TbContent tbContent){
        try{
            if(StringUtils.isEmpty(tbContent)){
                //为空
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            tbContent.setUpdated(new Date());
            tbContentService.updateById(tbContent);
            //添加成功
            return ResponseEntity.status(HttpStatus.CREATED).build();
        }catch (Exception e){
            LOGGER.error("修改商品失败！tbItem="+tbContent,e);
        }
        //修改失败！
        return ResponseEntity.status(HttpStatus.INSUFFICIENT_STORAGE).build();
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/delete")
    public  ResponseEntity<Void> deletContent(String id){
        EntityWrapper<TbContent> wrapper = new EntityWrapper<>();
        wrapper.where("id in("+id+")");
        tbContentService.delete(wrapper);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}

